package com.larry.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 16:22 2021/11/12
 * @Description TODO
 **/
public class HdfsClientTest {


    private FileSystem fs;

    /**
     * 初始化
     * @throws IOException
     * @throws InterruptedException
     */
    @Before
    public void init() throws IOException, InterruptedException, URISyntaxException {
        //访问路径
        URI uri = new URI("hdfs://192.168.10.102:8020");
        //配置对象
        Configuration conf = new Configuration();
        //用户
        String user = "larry";
        //
        fs = FileSystem.get(uri, conf, user);
    }

    /**
     * 关闭资源
     * @throws IOException
     */
    @After
    public void close() throws IOException {
        fs.close();
    }

    /**
     *
     */
    @Test
    public void testCreateHdfsClient() throws IOException, InterruptedException {
        //访问路径
        URI uri = URI.create("hdfs://192.168.10.102:8020");
        //配置对象
        Configuration conf = new Configuration();
        //用户
        String user = "larry";
        //
        FileSystem fs = FileSystem.get(uri, conf, user);
        System.out.println(fs.getName());
        fs.close();
    }

    /**
     * boolean delSrc,
     * boolean overwrite,
     * Path src,
     * Path dst
     */
    @Test
    public void testCopyFromLocal() throws IOException {

        fs.copyFromLocalFile(false,false,new Path("C:\\Users\\larry\\IdeaProjects\\hadoop\\HdfsClient\\src\\main\\resources\\test\\hello.txt"),new Path("/test"));

    }

}
